import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;

public class AirChance {

    static final String CONN_URL = "jdbc:oracle:thin:@im2ag-oracle.e.ujf-grenoble.fr:1521:ufrima";
    static final String USER = "debroizr";
    static final String PASSWD = "bd2013";

    public static void menu() {
        System.out.println("\n*** Choisir une action a effectuer : ***");
        System.out.println("0 : Quitter");
        System.out.println("1 : Ajout");
        System.out.println("2 : Modification");
        System.out.println("3 : Suppression");
        System.out.println("4 : Confirmation d'un vol");
        System.out.println("5 : Reservation");
        System.out.println("6 : Consultation des commandes clients");
    }

    public static void main(String args[]) throws SQLException, ParseException {
        
        try {

            int actionMenu;

            boolean exit = false;

            // Enregistrement du driver Oracle
            System.out.print("Loading Oracle driver... ");
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            System.out.println("loaded");

            // Etablissement de la connection
            System.out.print("Connecting to the database... ");
            Connection conn = DriverManager.getConnection(CONN_URL, USER, PASSWD);
            System.out.println("connected");

            // Desactivation de l'autocommit
            conn.setAutoCommit(false);
            System.out.println("Autocommit disabled");
            menu();

            while (!exit) {
                actionMenu = LectureClavier.lireEntier("Votre choix ?\n");
                switch (actionMenu) {
                    case 0:
                        exit = true;
                        System.out.println("\nAu revoir");
                        break;
                    case 1:
                        Ajout.ajout(conn);
                        break;
                    case 2:
                        Modification.modification(conn);
                        break;
                    case 3:
                        Suppression.suppression(conn);
                        break;
                    case 4:
                        Confirmation.confirmation(conn);
                        break;
                    case 5:
                        Reservation.reservation(conn);
                        break;
                    case 6:
                        Consultation.consultation(conn);
                        break;
                    case 7:
                        break;
                    case 8:
                        break;
                    default:
                        System.out.println("=> Choix incorrect");
                        menu();
                }
            }

            // Liberation des ressources et fermeture de la connexion...
            conn.close();

            // traitement d'exception
        } catch (SQLException e) {
            System.err.println("failed");
            System.out.println("Affichage de la pile d'erreur");
            // e.printStackTrace(System.err);
            System.out.println("Affichage du message d'erreur");
            System.out.println(e.getMessage());
            System.out.println("Affichage du code d'erreur");
            System.out.println(e.getErrorCode());
        }
    }

}
